//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    slider: [],
    yuerlist: [],

    page: 1,
    limit: 10,
  },
    // 跳转到播放
    goplay(e){
      console.log(e)
  
      wx.setStorage({
        key: 'tmusic',
        data: e.currentTarget.dataset,
        success(){
          wx.navigateTo({
            url: '../kAyejtplay/index',
          })
        }
      })
    },
  more: function () {
    wx.navigateTo({
      url: '../Ayejt/index'
    })
  },
  hbhd: function () {
    wx.navigateTo({
      url: '../picturebook/picturebook'
    })
  },
  student: function () {
    wx.navigateTo({
      url: '../studentlist/studentlist'
    })
  },
  hbyp: function () {
    wx.navigateTo({
      url: '../kAhbyp/index'
    })
    console.log("aaa")
  },

  getlist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res);
        let setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.pyuer,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  yuerlist: e.data.data.list
                })
                console.log("aaa")
                wx.hideLoading();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let yuerlist = that.data.yuerlist
                yuerlist = yuerlist.concat(e.data.data.list)
                that.setData({
                  yuerlist: yuerlist
                })
                wx.hideLoading()
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }

              // 空的

            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      },
      fail(e){
        console.log(e)
        let setdata = {
          // token: res.data.token,
          page: that.data.page,
          limit: that.data.limit
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.pyuer,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  yuerlist: e.data.data.list
                })
                console.log("aaa")
                wx.hideLoading();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let yuerlist = that.data.yuerlist
                yuerlist = yuerlist.concat(e.data.data.list)
                that.setData({
                  yuerlist: yuerlist
                })
                wx.hideLoading()
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }

              // 空的

            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar({})
    var that = this;
        wx.request({
          url: app.api + app.pslide,
          method: 'POST',
          success(e) {
            console.log(e);
            that.setData({
              slider: e.data.data.list
            })
          }
        })


    // 育儿讲堂
    that.getlist();

    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var number = that.data.page + 1;
    that.setData({
      page: number
    })
    console.log(that.data.page)
    that.getlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  gojy: function () {
    wx.reLaunch({
      url: '../Ajy/index'
    })
  },
  gohd: function () {
    wx.reLaunch({
      url: '../Ahd/index'
    })
  },
  gomine: function () {
    wx.reLaunch({
      url: '../Amine/index'
    })
  },
  
})